package com.huangmb.anymark.app.fragment.marker;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.NaviPara;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.*;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.huangmb.androidlib.utils.Logger;
import com.huangmb.anymark.app.R;
import com.huangmb.anymark.app.api.FileApi;
import com.huangmb.anymark.app.model.UserMarker;
import com.huangmb.common.utils.StringUtils;

/**
 * 美食详情页
 *
 * @author hmb
 * @version 1.0
 * @since 2016/4/24 10:24
 */
public class FoodShopFragment extends BaseUserMarkerFragment {
    private static final String TAG = FoodShopFragment.class.getSimpleName();
    private UserMarker marker;

    private ImageView mStaticMap;
    private TextView mAddress;
    private ProgressBar mProgressBar;
    private AlertDialog mDialog;

    public static FoodShopFragment newInstance(UserMarker marker) {
        FoodShopFragment foodShopFragment = new FoodShopFragment();
        Bundle b = new Bundle();
        b.putParcelable("marker", marker);
        foodShopFragment.setArguments(b);
        return foodShopFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_detail_food_shop, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mProgressBar = getProgressbar();

        marker = getUserMarker();
        if (marker == null) {
            return;
        }


        TextView title = (TextView) view.findViewById(R.id.title);
        title.setText(marker.getTitle());
        TextView info = (TextView) view.findViewById(R.id.info);
        info.setText(marker.getInfo());

        mStaticMap = (ImageView) view.findViewById(R.id.static_map);
        mAddress = (TextView) view.findViewById(R.id.address);

        double longitude = marker.getLongitude();
        double latitude = marker.getLatitude();
        String address = marker.getAddress();

        if (!StringUtils.isBlank(address)) {
            mAddress.setText(address);
        } else {
            //查询地理位置
            queryAddress(mAddress, latitude, longitude);
        }

        //请求静态地图
        queryStaticMap(mStaticMap, latitude, longitude);
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setMessage("是否打开高德地图进入导航");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                NaviPara para = new NaviPara();
                LatLng latLng = new LatLng(marker.getLatitude(), marker.getLongitude());
                para.setTargetPoint(latLng);
                try {
                    AMapUtils.openAMapNavi(para, getContext());
                } catch (com.amap.api.maps.AMapException e) {
                    toast("无法打开地图应用");
                }
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        mDialog = builder.create();
        mStaticMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.show();
            }
        });

        //请求封面图片
        if (StringUtils.isNotBlank(marker.getCoverImage())) {
            final ImageView cover = (ImageView) view.findViewById(R.id.cover);
            RequestQueue queue = Volley.newRequestQueue(getContext());
            String url = FileApi.getDownloadUrl(marker.getCoverImage());
            ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    mProgressBar.setVisibility(View.GONE);
                    cover.setImageBitmap(response);
                }
            }, 0, 0, ImageView.ScaleType.CENTER_INSIDE, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
            mProgressBar.setVisibility(View.VISIBLE);
            queue.add(request);
        } else {
            mProgressBar.setVisibility(View.GONE);
        }
    }

}
